﻿/* 
#-------------------------------------------------------------------------------
# Name:        SettingsForm - This Form will display the settings for the visionAlgorithms
#
# Usage:       The usage for this Form will be documented in a readme file
#              Uses the CreateControls() Function of every VisionAlgorithm passed on to get its controls
#              Saves the changed settings using the SetSettings() function
# 
#
# Requires:    Visual Studio 2010 and up.
#
# Author:      Roy Scheefhals
#
# Created:     30-03-2012
#-------------------------------------------------------------------------------
*/


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibraryCS;
using System.Diagnostics;

namespace Eindproject
{
    public partial class SettingsForm : Form
    {

        List<VisionAlgorithm> VisionAlgorithmList;

        private bool propertychanged = false;
        private VisionAlgoViewItem SelectedAlgoViewItem;
        private int SelectedViewItemIndex;

        /// <summary>
        /// Constructor for the settingsform
        /// </summary>
        /// <param name="VIList">VisionAlgorithm list containing algorithms</param>
        public SettingsForm(List<VisionAlgorithm> VIList)
        {
            VisionAlgorithmList = VIList;
            InitializeComponent();
            InitializeSettings();
        }

        private void InitializeSettings()
        {
            this.SuspendLayout();
            VisionAlgoViewItem VAVI;
            foreach (VisionAlgorithm VI in VisionAlgorithmList)
            {
                VAVI = new VisionAlgoViewItem(VI.Name, VI.createControls());
                foreach (List<Control> ControlsPerLine in VAVI.propertyfield.SettingsPanel)
                {
                    foreach (Control c in ControlsPerLine)
                    {
                        if (c is TextBox)
                            c.TextChanged += new EventHandler(genericControl_Changed);
                        if (c is CheckBox)// eventhandler to know if changes have been make
                            (c as CheckBox).CheckedChanged += new EventHandler(genericControl_Changed);
                        if (c is Button)
                        {
                            Button b = (Button)c;
                            if (b.Name.Equals("OFD"))
                            { // if button his purpose is for an Open File Dialog .. add new event handler
                                b.Click += new EventHandler(genericOFDButton_Clicked);
                            }
                        }
                    }
                }
                AlgorithmsList.Items.Add(VAVI);
            }
            this.ResumeLayout();
        }

        private void AlgorithmsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (AlgorithmsList.SelectedItems.Count == 0)
            {
                if (propertychanged)
                //previous settings have been changed--save changed items
                {
                    List<object> objectsToPass = new List<object>();
                    String nameToFind = SelectedAlgoViewItem.name;
                    VisionAlgorithm SelectedAlgo = VisionAlgorithmList.Find(delegate(VisionAlgorithm VA) { return VA.Name == nameToFind; });

                    foreach (List<Control> ControlLine in SelectedAlgoViewItem.propertyfield.SettingsPanel)
                    {
                        foreach (Control c in ControlLine)
                        {
                            if (c is TextBox)
                            {
                                if (c.Text != "")
                                    objectsToPass.Add(c.Text);
                                else
                                {
                                    MessageBox.Show("Some Fields have not been filled yet\nSettings have not been applied", "Check Fields pl0x", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                    propertychanged = false;
                                    return;
                                }
                            }
                            if (c is CheckBox)
                                objectsToPass.Add((c as CheckBox).Checked);
                            
                            if (c is Button)
                            {
                                Button b = (Button)c;
                                if (b.Name.Equals("OFD"))
                                {

                                }
                            }
                        }
                    }
                    SelectedAlgo.setSettings(objectsToPass);
                    propertychanged = false;
                }
            }

            else
            { // Change to the new selected View item and get the right controls
                SelectedAlgoViewItem = (VisionAlgoViewItem)AlgorithmsList.SelectedItems[0];
                SelectedViewItemIndex = AlgorithmsList.SelectedIndices[0];

                SettingsTable.Controls.Clear();
                for (int LineIndex = 0; LineIndex < SelectedAlgoViewItem.propertyfield.SettingsPanel.Count; LineIndex++)
                {// for every propertyline
                    for (int ControlIndex = 0; ControlIndex < SelectedAlgoViewItem.propertyfield.SettingsPanel[LineIndex].Count; ControlIndex++)
                    {// for every control in this propertyline ( max 4 controls) 
                        List<Control> controlLists = SelectedAlgoViewItem.propertyfield.SettingsPanel[LineIndex];

                        SettingsTable.Controls.Add(controlLists[ControlIndex], ControlIndex, LineIndex);
                    }
                }
            }
        }

        private void genericControl_Changed(object sender, EventArgs arguments)
        {
            propertychanged = true;
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (propertychanged)
            {
                DialogResult result = MessageBox.Show("Save current changes?", "Items Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (result == DialogResult.Yes)
                {
                    List<object> objectsToPass = new List<object>();
                    String nameToFind = SelectedAlgoViewItem.name;
                    VisionAlgorithm SelectedAlgo = VisionAlgorithmList.Find(delegate(VisionAlgorithm VA) { return VA.Name == nameToFind; });

                    foreach (List<Control> ControlLine in SelectedAlgoViewItem.propertyfield.SettingsPanel)
                    {
                        foreach (Control c in ControlLine)
                        {
                            if (c is TextBox)
                            {
                                if (c.Text != "")
                                    objectsToPass.Add(c.Text);
                                else
                                {
                                    MessageBox.Show("Some Fields have not been filled yet\nSettings have not been applied", "Check Fields", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                    propertychanged = false;
                                    return;
                                }
                            }
                            if (c is CheckBox)
                                objectsToPass.Add((c as CheckBox).Checked);
                        }
                    }
                    SelectedAlgo.setSettings(objectsToPass);
                }
                propertychanged = false;
            }
        }

        private void saveChangesMenuItem_Clicked(object sender, EventArgs e)
        {
            if (propertychanged)
            {

                List<object> objectsToPass = new List<object>();
                String nameToFind = SelectedAlgoViewItem.name;
                VisionAlgorithm SelectedAlgo = VisionAlgorithmList.Find(delegate(VisionAlgorithm VA) { return VA.Name == nameToFind; });

                foreach (List<Control> ControlLine in SelectedAlgoViewItem.propertyfield.SettingsPanel)
                {
                    foreach (Control c in ControlLine)
                    {
                        if (c is TextBox)
                            objectsToPass.Add(c.Text);
                        if (c is CheckBox)
                            objectsToPass.Add((c as CheckBox).Checked);
                    }
                }
                SelectedAlgo.setSettings(objectsToPass);
                propertychanged = false;
            }
        }

        //Open file dialog button has been clicked, open OFD.. then put the full path + filename into the textbox 1 control before the button
        private void genericOFDButton_Clicked(object sender, EventArgs e)
        {
            TextBox CorrespondingTXTFIELD = new TextBox();
            for (int LineIndex = 0; LineIndex < SelectedAlgoViewItem.propertyfield.SettingsPanel.Count; LineIndex++)
                {// for every propertyline

                    List<Control> controlLists = SelectedAlgoViewItem.propertyfield.SettingsPanel[LineIndex];

                    for (int ControlIndex = 0; ControlIndex < SelectedAlgoViewItem.propertyfield.SettingsPanel[LineIndex].Count; ControlIndex++)
                    {// for every control in this propertyline ( max 4 controls)
                        Control curControl = controlLists[ControlIndex];
                        if (curControl is Button)
                        {
                            CorrespondingTXTFIELD = (TextBox)controlLists[ControlIndex - 1];
                        }
                    }
                }
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Learned.txt|Learned.txt";
            ofd.Multiselect = false;
            ofd.CheckPathExists = true;
            ofd.CheckFileExists = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                CorrespondingTXTFIELD.Text = ofd.FileName;
            }
        }
    }
}
